package com.esdk.sql.orm;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.esdk.exception.SdkRuntimeException;
import com.esdk.sql.IConnectionable;
import com.esdk.sql.ISelect;
import com.esdk.utils.EasySql;
import org.apache.poi.ss.formula.functions.T;

public class ORMSession implements IConnectionable{
  HashMap<String,Object> selectMap=new HashMap();
  Connection conn;
  public ORMSession(Connection con){
    setConnection(con);
    ORMSessionFactory.addORMSession(this);
  }
  
  public void setAutoCommit(boolean b) throws SQLException {
    conn.setAutoCommit(b);
  }
  
  public void close(){
  	clear();
    ORMSessionFactory.removeORMSession(this);
    EasySql.close(conn);
  }
  
  public void clear() {
    selectMap.clear();
  }

  public <T extends ParentRow> ParentResultSet<T> putParentResultSet(AbstractSelect select,ParentResultSet<T> rs) {
    selectMap.put("ParentResultSet:"+select.getSQL(),rs);
    return rs;
  }

  public <T extends ParentRow> ParentResultSet<T> getParentResultSet(AbstractSelect select) {
    return (ParentResultSet<T>)selectMap.get("ParentResultSet:"+select.getSQL());
  }

  public ABResultSet putABResultSet(ISelect select,ABResultSet rs) {
    selectMap.put("ABResultSet:"+select.getSQL(),rs);
    return rs;
  }

  public ABResultSet getABResultSet(ISelect select) {
    return (ABResultSet)selectMap.get("ABResultSet:"+select.getSQL());
  }

  public ABRowSet putABRowSet(ISelect select,ABRowSet rs) {
    selectMap.put("ABRowSet:"+select.getSQL(),rs);
    return rs;
  }

  public ABRowSet getABRowSet(ISelect select) {
    return (ABRowSet)selectMap.get("ABRowSet:"+select.getSQL());
  }

  public ABRow putABRow(ISelect select,ABRow row) {
    selectMap.put("ABRow:"+select.getSQL(),row);
    return row;
  }

  public ABRow getABRow(ISelect select) {
    return (ABRow)selectMap.get("ABRow:"+select.getSQL());
  }

  public void remove(ISelect select) {
    for(String key:selectMap.keySet().toArray(new String[0])){
      if(key.contains(select.getSQL()))
        selectMap.remove(key);
    }
  }

  public void setConnection(Connection con){conn=con;}
  
  public Connection getConnection() {
    return conn;
  }

}
